// api.js数据接口
import axios from 'axios'
import {getToken, removeToken} from "./auth";
import configJs from '@/config'
import {Toast, Dialog} from "vant";

export function request (self, config) {
    // console.log(config)
    config.baseURL = configJs.api.apiRootPath
    config.timeout = 5000
    let token = getToken()
    // console.log(token)
    let headers = {}
    if (token) {
        headers[configJs.api.tokenKey] = token
    }
    if (configJs.api.debugAddress !== null && configJs.api.debugAddress !== '') {
        headers['DebugAddress'] = configJs.api.debugAddress
    }
    if (headers.length !== 0) {
        let oldHeaders = config['headers']
        if (oldHeaders == null) {
            config['headers'] = headers
        } else {
            config['headers'] = Object.assign(oldHeaders, headers)
        }
    }
    // console.log(config)
    let resp = axios(config)
    if (resp) {
        resp.then(response => {
            // console.log(response)
            if (response.status) {
                // console.log(2)
                if (response.status === 200) {
                    // console.log(response.data)
                    if (response.data.code === configJs.errCode.errOk) {
                    } else if (response.data.code.startsWith('0')) {
                        // console.log(response.data)
                    } else if (response.data.code === configJs.errCode.errLogin) {
                        removeToken(configJs.api.tokenKey)
                        Dialog.alert({
                            title: self.$t('messages.t1'),
                            confirmButtonText: self.$t('messages.t9')
                        }).then(() => {
                            self.$router.push({path: '/login'})
                        });
                        return
                    } else if (response.data.code === configJs.errCode.errNeedLogin) {
                        // console.log(response.data)
                        Dialog.alert({
                            title: self.$t('messages.t2'),
                            confirmButtonText: self.$t('messages.t9')
                        }).then(() => {
                            self.$router.push({path: '/login'})
                        });
                        return
                    }
                    if (response.data.msg !== null && response.data.msg !== '') {
                        Toast(response.data.msg);
                    }
                } else {
                    console.log('不支持的返回状态')
                }
            } else {
                console.log('没有返回状态')
                // console.log(resp)
            }
        }).catch(function (error) {
            // console.log(error.response + '11')
            if (error.response) {
                if (error.response.status) {
                    let status = error.response.status + ''
                    if (status === '404') {
                        Toast.fail(self.$t('messages.t3'));
                    } else if (status === '401') {
                        Dialog.alert({
                            title: self.$t('messages.t2'),
                            confirmButtonText: self.$t('messages.t9')
                        }).then(() => {
                            self.$router.push({path: '/login'})
                        });
                        return
                    } else if (status === '405') {
                        Toast.fail(self.$t('messages.t4'));
                    } else if (status.startsWith('5')) {
                        Toast.fail(self.$t('messages.t5'));
                    } else {
                        Toast.fail(self.$t('messages.t6'));
                    }
                } else {
                    // 没有状态
                    console.log(error.config)
                }
            } else {
                // 404,网络不通,401
                console.log(error.message)
                if (error.message.includes('timeout')) {
                    Toast.fail(self.$t('messages.t7'));
                } else {
                    Toast.fail(self.$t('messages.t8'));
                }
            }
            return error
        })
    } else {
        console.log('没有返回结果')
    }
    return resp
}
